package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.api.RemoteSerialNumberUtils;
import com.zp.common.core.constant.BaseConstants;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.constant.DrugConstants;
import com.zp.common.core.constant.UserConstants;
import com.zp.common.core.utils.DateUtils;
import com.zp.common.core.utils.SnowIdUtils;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import com.zp.drug.api.vo.DrugInfoVo;
import com.zp.drug.entity.*;
import com.zp.drug.entity.bo.*;
import com.zp.drug.entity.vo.DrugInventoryCheckVo;
import com.zp.drug.entity.vo.DrugStockAllLogParamVo;
import com.zp.drug.entity.vo.DrugStockVo;
import com.zp.drug.mapper.*;
import com.zp.drug.service.IDrugInventoryCheckService;
import com.zp.drug.service.IDrugStockAllLogService;
import com.zp.system.api.entity.SysDictData;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 药品盘点表Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class DrugInventoryCheckServiceImpl implements IDrugInventoryCheckService {

    // 规格单位
    static List<SysDictData> SPEC_UNIT_MAP = CacheUtils.get(CacheNames.SYS_DICT, CacheNames.SPEC_UNIT);
    // 药品基本信息
    static Map<String, DrugInfoVo> drugInfoVoMap = CacheUtils.get(CacheNames.DRUG_INFO);
    private final DrugInventoryCheckMapper baseMapper;
    private final DrugInventoryCheckPreparationMapper drugInventoryCheckPreparationMapper;
    private final DrugStockMapper drugStockMapper;
    private final DrugExportMasterMapper drugExportMasterMapper;
    private final DrugExportDetailMapper drugExportDetailMapper;
    private final DrugImportMasterMapper drugImportMasterMapper;
    private final DrugImportDetailMapper drugImportDetailMapper;
    private final IDrugStockAllLogService stockAllLogService;
    @DubboReference
    private RemoteSerialNumberUtils remoteSerialNumberUtils;

    /**
     * 查询药品盘点表
     */
    @Override
    public DrugInventoryCheckVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药品盘点表列表(分页)
     */
    @Override
    public TableDataInfo<DrugInventoryCheckVo> queryPageList(DrugInventoryCheckBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DrugInventoryCheck> lqw = buildQueryWrapper(bo);
        Page<DrugInventoryCheckVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品盘点表列表(不分页)
     */
    @Override
    public List<DrugInventoryCheckVo> queryList(DrugInventoryCheckBo bo) {
        LambdaQueryWrapper<DrugInventoryCheck> lqw = buildQueryWrapper(bo);
        List<DrugInventoryCheckVo> drugInventoryCheckVos = baseMapper.selectVoList(lqw);
        drugInventoryCheckVos.forEach(item -> {
            item.setPackageUnitName(CollectionUtil.isNotEmpty(SPEC_UNIT_MAP)
                    && StringUtils.isNotBlank(item.getPackageUnits()) ?
                    SPEC_UNIT_MAP.stream()
                            .filter(i -> StringUtils.equals(item.getPackageUnits(), i.getDictValue()))
                            .findAny().get().getDictLabel() : "");
        });
        return drugInventoryCheckVos;
    }

    private LambdaQueryWrapper<DrugInventoryCheck> buildQueryWrapper(DrugInventoryCheckBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrugInventoryCheck> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCheckYearMonth() != null, DrugInventoryCheck::getCheckYearMonth, bo.getCheckYearMonth());
        lqw.eq(StringUtils.isNotBlank(bo.getInventoryNo()), DrugInventoryCheck::getInventoryNo, bo.getInventoryNo());
        lqw.eq(StringUtils.isNotBlank(bo.getSaveType()), DrugInventoryCheck::getSaveType, bo.getSaveType());
        lqw.eq(bo.getItemNo() != null, DrugInventoryCheck::getItemNo, bo.getItemNo());
        lqw.eq(StringUtils.isNotBlank(bo.getStockId()), DrugInventoryCheck::getStockId, bo.getStockId());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugCode()), DrugInventoryCheck::getDrugCode, bo.getDrugCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugClass()), DrugInventoryCheck::getDrugClass, bo.getDrugClass());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugInfoId()), DrugInventoryCheck::getDrugInfoId, bo.getDrugInfoId());
        lqw.eq(StringUtils.isNotBlank(bo.getStorageId()), DrugInventoryCheck::getStorageId, bo.getStorageId());
        lqw.eq(bo.getPackageNum() != null, DrugInventoryCheck::getPackageNum, bo.getPackageNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageSpec()), DrugInventoryCheck::getPackageSpec, bo.getPackageSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageUnits()), DrugInventoryCheck::getPackageUnits, bo.getPackageUnits());
        lqw.eq(bo.getPurchasePrice() != null, DrugInventoryCheck::getPurchasePrice, bo.getPurchasePrice());
        lqw.eq(bo.getTradePrice() != null, DrugInventoryCheck::getTradePrice, bo.getTradePrice());
        lqw.eq(bo.getRetailPrice() != null, DrugInventoryCheck::getRetailPrice, bo.getRetailPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), DrugInventoryCheck::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getExpireDate() != null, DrugInventoryCheck::getExpireDate, bo.getExpireDate());
        lqw.eq(bo.getAccountQuantity() != null, DrugInventoryCheck::getAccountQuantity, bo.getAccountQuantity());
        lqw.eq(bo.getActualQuantity() != null, DrugInventoryCheck::getActualQuantity, bo.getActualQuantity());
        lqw.eq(bo.getMaxQuantity() != null, DrugInventoryCheck::getMaxQuantity, bo.getMaxQuantity());
        lqw.eq(bo.getMinQuantity() != null, DrugInventoryCheck::getMinQuantity, bo.getMinQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), DrugInventoryCheck::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getSolidFill()), DrugInventoryCheck::getSolidFill, bo.getSolidFill());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve2()), DrugInventoryCheck::getReserve2, bo.getReserve2());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve3()), DrugInventoryCheck::getReserve3, bo.getReserve3());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve4()), DrugInventoryCheck::getReserve4, bo.getReserve4());
        lqw.eq(StringUtils.isNotBlank(bo.getMinUnits()), DrugInventoryCheck::getMinUnits, bo.getMinUnits());
        lqw.eq(StringUtils.isNotBlank(bo.getMinSpec()), DrugInventoryCheck::getMinSpec, bo.getMinSpec());
        lqw.eq(bo.getAccountMoney() != null, DrugInventoryCheck::getAccountMoney, bo.getAccountMoney());
        lqw.eq(bo.getActualMoney() != null, DrugInventoryCheck::getActualMoney, bo.getActualMoney());
        lqw.eq(bo.getAccountMoneySales() != null, DrugInventoryCheck::getAccountMoneySales, bo.getAccountMoneySales());
        lqw.eq(bo.getActualMoneySales() != null, DrugInventoryCheck::getActualMoneySales, bo.getActualMoneySales());
        lqw.eq(bo.getProfitMoneySales() != null, DrugInventoryCheck::getProfitMoneySales, bo.getProfitMoneySales());
        return lqw;
    }

    /**
     * 新增药品盘点表
     */
    @Override
    @GlobalTransactional
    public Boolean insertByBo(DrugInventoryCheckBo bo) {
        String drugInventoryNo = "";
        Date checkYearMonth = null;
        Date checkYearMonthEnd = null;
        if (StringUtils.equals(bo.getSaveType(), "0")) {
            drugInventoryNo = bo.getInventoryNo();
            checkYearMonth = DateUtils.dataFormat(DateUtils.YYYY_MM_DD, bo.getCheckYearMonth());
            checkYearMonthEnd = bo.getDrugInventoryCheckBoList().get(0).getCheckYearMonthEnd();
            // 删除预盘点表数据
            drugInventoryCheckPreparationMapper.delete(Wrappers.<DrugInventoryCheckPreparation>lambdaUpdate()
                    .eq(DrugInventoryCheckPreparation::getInventoryNo, drugInventoryNo)
                    .eq(DrugInventoryCheckPreparation::getOrgId, bo.getOrgId())
            );
        } else {
            drugInventoryNo = remoteSerialNumberUtils.generateDrugInventoryNo(bo.getOrgId());
            checkYearMonth = DateUtils.dataFormat(DateUtils.YYYY_MM_DD, DateUtils.getNowDate());
            checkYearMonthEnd = DateUtils.getNowDate();
        }
        List<DrugInventoryCheckBo> boList = bo.getDrugInventoryCheckBoList();
        for (DrugInventoryCheckBo itemBo : boList) {
            itemBo.setInventoryNo(drugInventoryNo);
            itemBo.setCheckYearMonth(checkYearMonth);
            itemBo.setSaveType("1");
            itemBo.setPurchasePrice(itemBo.getTradePrice());
            itemBo.setCheckYearMonthEnd(checkYearMonthEnd);
        }
        return baseMapper.insertBatch(BeanUtil.copyToList(boList, DrugInventoryCheck.class));
    }

    /**
     * 修改药品盘点表
     */
    @Override
    public Boolean updateByBo(DrugInventoryCheckBo bo) {
        List<DrugInventoryCheckBo> boList = bo.getDrugInventoryCheckBoList();
        return baseMapper.insertOrUpdateBatch(BeanUtil.copyToList(boList, DrugInventoryCheck.class));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DrugInventoryCheck entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品盘点表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 通过盘点号删除
     *
     * @param inventoryNo
     * @return
     */
    @Override
    public Boolean deleteWithValidByInventoryNo(String inventoryNo, String orgId) {
        return baseMapper.delete(Wrappers.<DrugInventoryCheck>lambdaUpdate()
                .eq(DrugInventoryCheck::getInventoryNo, inventoryNo)
                .eq(DrugInventoryCheck::getOrgId, orgId)
        ) > 0;
    }


    /**
     * 按申请单分组查询盘点记录
     *
     * @param bo
     * @return
     */
    @Override
    public TableDataInfo<DrugInventoryCheckVo> selectGroupInventoryNo(DrugInventoryCheckBo bo, PageQuery pageQuery) {
        Page<DrugInventoryCheckVo> result = baseMapper.selectGroupInventoryNo(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 库存调整
     *
     * @param inventoryNo
     * @param orgId
     * @return
     */
    @Override
    @GlobalTransactional
    public Boolean stockAdjust(String inventoryNo, String orgId) {
        List<DrugInventoryCheckVo> drugInventoryCheckVos = baseMapper.selectVoList(Wrappers.<DrugInventoryCheck>lambdaQuery()
                .eq(DrugInventoryCheck::getInventoryNo, inventoryNo)
                .eq(DrugInventoryCheck::getOrgId, orgId)
        );
        List<DrugInventoryCheckVo> collect = drugInventoryCheckVos.stream().filter(e -> e.getProfitQuantity() != 0).collect(Collectors.toList());
        Map<Boolean, List<DrugInventoryCheckVo>> groupedByValue = collect.stream()
                .collect(Collectors.groupingBy(
                        item -> item.getProfitQuantity() > 0,
                        Collectors.mapping(Function.identity(), Collectors.toList())
                ));
        // 正数分组
        List<DrugInventoryCheckVo> positiveItems = groupedByValue.getOrDefault(true, Collections.emptyList());
        // 负数分组
        List<DrugInventoryCheckVo> negativeItems = groupedByValue.getOrDefault(false, Collections.emptyList());
        processingExportItems(negativeItems, orgId);
        processingImportItems(positiveItems, orgId);
        return Boolean.TRUE;
    }

    /**
     * 盘亏出库
     *
     * @param negativeItems
     * @param orgId
     */
    @Transactional
    public void processingExportItems(List<DrugInventoryCheckVo> negativeItems, String orgId) {
        if (CollectionUtil.isNotEmpty(negativeItems)) {
            List<DrugExportDetail> drugExportDetailList = new ArrayList<>();
            DrugInventoryCheckVo vo = negativeItems.get(0);
            String documentNo = remoteSerialNumberUtils.generateDrugExportNo(orgId, "pkck");
            String exportId = SnowIdUtils.uniqueString();
            DrugExportMaster drugExportMaster = new DrugExportMaster();
            drugExportMaster.setExportId(exportId);
            drugExportMaster.setDocumentNo(documentNo);
            drugExportMaster.setStorageId(vo.getStorageId());
            drugExportMaster.setExportDate(DateUtils.getNowDate());
            drugExportMaster.setReceiverId(vo.getStorageId());
            drugExportMaster.setReceiverUserId(vo.getUserId());
            drugExportMaster.setExportClass(DrugConstants.DRUG_STORAGE_TYPE.TAKE_STOCK_DRUG);
            drugExportMaster.setAccountType("1");
            drugExportMaster.setOrgId(orgId);
            drugExportMaster.setIfBatch(UserConstants.YES);
            drugExportMaster.setImportMasterId(vo.getUserId());
            drugExportMaster.setImportDate(DateUtils.getNowDate());
            drugExportMaster.initEntity();
            drugExportMaster.setAccountReceivable(negativeItems.stream()
                    .map(DrugInventoryCheckVo::getProfitMoneySales)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
            drugExportMaster.setAccountPayed(drugExportMaster.getAccountReceivable());
            negativeItems.forEach(item -> {
                int profitQuantity = Math.abs(item.getProfitQuantity());
                DrugStockVo drugStockVo = drugStockMapper.selectVoOne(Wrappers.<DrugStock>lambdaQuery()
                        .eq(DrugStock::getBatchNo, item.getBatchNo())
                        .eq(DrugStock::getStorageId, item.getStorageId())
                        .eq(DrugStock::getTradePriceMin, item.getTradePriceMin())
                        .eq(DrugStock::getRetailPriceMin, item.getRetailPriceMin())
                        .eq(DrugStock::getOrgId, orgId)
                );
                // 修改库存（扣减）
                drugStockMapper.update(null, Wrappers.<DrugStock>lambdaUpdate()
                        .set(DrugStock::getQuantity, drugStockVo.getQuantity() - profitQuantity)
                        .eq(DrugStock::getId, drugStockVo.getId())
                );
                DrugExportDetail drugExportDetail = BeanUtil.toBean(item, DrugExportDetail.class);
                drugExportDetail.setMasterId(exportId);
                drugExportDetail.setStockId(drugStockVo.getId());
                drugExportDetail.setDiscount(BigDecimal.valueOf(1));
                drugExportDetail.setProduceDate(drugStockVo.getProduceDate());
                drugExportDetail.setQuantity(profitQuantity);
                drugExportDetail.setSurplusInventory(drugStockVo.getQuantity() - drugExportDetail.getQuantity());
                drugExportDetail.setAllInventory(drugStockVo.getQuantity());
                drugExportDetail.setRevokeType(UserConstants.NO);
                drugExportDetail.setDrugPriceId(drugStockVo.getDrugPriceId());
                drugExportDetail.setPackSize(BaseConstants.IS_SMALL_PACK);
                drugExportDetail.initEntity();
                drugExportDetail.setUserId(vo.getUserId());
                drugExportDetailList.add(drugExportDetail);
            });
            DrugExportMasterBo drugExportMasterBo = BeanUtil.toBean(drugExportMaster, DrugExportMasterBo.class);
            drugExportMasterBo.setDetailListSave(BeanUtil.copyToList(drugExportDetailList, DrugExportDetailBo.class));
            drugExportMasterMapper.insert(drugExportMaster);
            drugExportDetailMapper.insertBatch(drugExportDetailList);
            processingStockLog(drugExportMasterBo, null, "export");
        }
    }

    /**
     * 盘盈入库
     *
     * @param positiveItems
     * @param orgId
     */
    @Transactional
    public void processingImportItems(List<DrugInventoryCheckVo> positiveItems, String orgId) {
        if (CollectionUtil.isNotEmpty(positiveItems)) {
            List<DrugImportDetail> drugImportDetailList = new ArrayList<>();
            DrugInventoryCheckVo vo = positiveItems.get(0);
            String documentNo = remoteSerialNumberUtils.generateDrugImportNo(orgId, "pyrk");
            String importId = SnowIdUtils.uniqueString();
            DrugImportMaster drugImportMaster = new DrugImportMaster();
            drugImportMaster.setId(importId);
            drugImportMaster.setDocumentNo(documentNo);
            drugImportMaster.setStorageId(vo.getStorageId());
            drugImportMaster.setImportDate(DateUtils.getNowDate());
            drugImportMaster.setStorageFrom(vo.getStorageId());
            drugImportMaster.setImportClass(DrugConstants.DRUG_STORAGE_TYPE.TAKE_STOCK_DRUG);
            drugImportMaster.setAccountType("1");
            drugImportMaster.setOrgId(orgId);
            drugImportMaster.setImportDate(DateUtils.getNowDate());
            drugImportMaster.initEntity();
            drugImportMaster.setAccountReceivable(positiveItems.stream()
                    .map(DrugInventoryCheckVo::getProfitMoneySales)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
            drugImportMaster.setAccountPayed(drugImportMaster.getAccountReceivable());
            positiveItems.forEach(item -> {
                int profitQuantity = Math.abs(item.getProfitQuantity());
                DrugStockVo drugStockVo = drugStockMapper.selectVoOne(Wrappers.<DrugStock>lambdaQuery()
                        .eq(DrugStock::getBatchNo, item.getBatchNo())
                        .eq(DrugStock::getStorageId, item.getStorageId())
                        .eq(DrugStock::getTradePriceMin, item.getTradePriceMin())
                        .eq(DrugStock::getRetailPriceMin, item.getRetailPriceMin())
                        .eq(DrugStock::getOrgId, orgId)
                );
                // 修改库存（增加）
                drugStockMapper.update(null, Wrappers.<DrugStock>lambdaUpdate()
                        .set(DrugStock::getQuantity, drugStockVo.getQuantity() + profitQuantity)
                        .eq(DrugStock::getId, drugStockVo.getId())
                );
                DrugImportDetail drugimportDetail = BeanUtil.toBean(item, DrugImportDetail.class);
                drugimportDetail.setMasterId(importId);
                drugimportDetail.setStockId(drugStockVo.getId());
                drugimportDetail.setDiscount(BigDecimal.valueOf(1));
                drugimportDetail.setProduceDate(drugStockVo.getProduceDate());
                drugimportDetail.setQuantity(profitQuantity);
                drugimportDetail.setPackSize(BaseConstants.IS_SMALL_PACK);
                drugimportDetail.initEntity();
                drugimportDetail.setAllStock(drugStockVo.getQuantity());

                drugimportDetail.setUserId(vo.getUserId());
                drugImportDetailList.add(drugimportDetail);
            });
            DrugImportMasterBo drugImportMasterBo = BeanUtil.toBean(drugImportMaster, DrugImportMasterBo.class);
            drugImportMasterBo.setDetailListSave(BeanUtil.copyToList(drugImportDetailList, DrugImportDetailBo.class));
            drugImportMasterMapper.insert(drugImportMaster);
            drugImportDetailMapper.insertBatch(drugImportDetailList);
            processingStockLog(null, null, "import");
        }
    }

    @Transactional
    public void processingStockLog(DrugExportMasterBo drugExportMasterBo, DrugImportMasterBo drugImportMasterBo, String type) {

        if (StringUtils.equals(type, "export")) {
            DrugStockAllLog allLog = new DrugStockAllLog();
            allLog.setId(SnowIdUtils.uniqueString());
            allLog.setRecordType(DrugConstants.ELEVEN);
            allLog.setSourceType(DrugConstants.DRUG_STORAGE_TYPE.TAKE_STOCK_DRUG);
            allLog.setDocumentNo(drugExportMasterBo.getDocumentNo());
            List<DrugExportDetailBo> detailListSaveList = drugExportMasterBo.getDetailListSave();
            detailListSaveList.forEach(detailBo -> {
                // 查询对应药品的药品info信息
                DrugInfoVo drugInfoVo = drugInfoVoMap.get(detailBo.getDrugInfoId());
                allLog.setDrugInfoId(detailBo.getDrugInfoId());
                allLog.setStockId(detailBo.getStockId());
                allLog.setDrugName(drugInfoVo.getDrugName());
                allLog.setDrugCode(drugInfoVo.getDrugCode());
                allLog.setSpec(detailBo.getMinSpec());
                allLog.setUnits(detailBo.getMinUnits());
                allLog.setPackageNum(detailBo.getPackageNum());
                allLog.setPriceId(detailBo.getDrugPriceId());
                allLog.setPackSize(detailBo.getPackSize());
                allLog.setAmount(detailBo.getQuantity());
                allLog.setCurrentAmount(detailBo.getAllInventory());
                allLog.setPriceQuotiety(BigDecimal.valueOf(1));
                allLog.setItemPrice(detailBo.getRetailPriceMin());
                allLog.setTradePrice(detailBo.getTradePriceMin());
                allLog.setCosts(detailBo.getRetailPriceMin().multiply(BigDecimal.valueOf(detailBo.getQuantity())));
                allLog.setApplyDept(drugExportMasterBo.getStorageId());
                allLog.setApplyPerson(drugExportMasterBo.getUserId());
                allLog.setRecordDept(drugExportMasterBo.getStorageId());
                allLog.setRecordPerson(drugExportMasterBo.getUserId());
                allLog.initEntity();
                allLog.setOrgId(drugExportMasterBo.getOrgId());
                DrugStockAllLogParamVo drugStockAllLogParamVo = new DrugStockAllLogParamVo();
                drugStockAllLogParamVo.setDrugStockAllLog(allLog);
                drugStockAllLogParamVo.setRecordType(DrugConstants.ELEVEN);
                Boolean b = stockAllLogService.caseRecordTypeSave(drugStockAllLogParamVo);
            });

        } else {
            DrugStockAllLog allLog = new DrugStockAllLog();
            allLog.setId(SnowIdUtils.uniqueString());
            allLog.setRecordType(DrugConstants.ELEVEN);
            allLog.setSourceType(DrugConstants.DRUG_STORAGE_TYPE.TAKE_STOCK_DRUG);
            allLog.setDocumentNo(drugImportMasterBo.getDocumentNo());
            List<DrugImportDetailBo> detailListSaveList = drugImportMasterBo.getDetailListSave();
            detailListSaveList.forEach(detailBo -> {
                // 查询对应药品的药品info信息
                DrugInfoVo drugInfoVo = drugInfoVoMap.get(detailBo.getDrugInfoId());
                allLog.setDrugInfoId(detailBo.getDrugInfoId());
                allLog.setStockId(detailBo.getStockId());
                allLog.setDrugName(drugInfoVo.getDrugName());
                allLog.setDrugCode(drugInfoVo.getDrugCode());
                allLog.setSpec(detailBo.getMinSpec());
                allLog.setUnits(detailBo.getMinUnits());
                allLog.setPackageNum(detailBo.getPackageNum());
                allLog.setPriceId(detailBo.getDrugPriceId());
                allLog.setPackSize(detailBo.getPackSize());
                allLog.setAmount(detailBo.getQuantity());
//                allLog.setCurrentAmount(detailBo.getAllInventory());
                allLog.setPriceQuotiety(BigDecimal.valueOf(1));
                allLog.setItemPrice(detailBo.getRetailPriceMin());
                allLog.setTradePrice(detailBo.getTradePriceMin());
                allLog.setCosts(detailBo.getRetailPriceMin().multiply(BigDecimal.valueOf(detailBo.getQuantity())));
                allLog.setApplyDept(drugImportMasterBo.getStorageId());
                allLog.setApplyPerson(drugImportMasterBo.getUserId());
                allLog.setRecordDept(drugImportMasterBo.getStorageId());
                allLog.setRecordPerson(drugImportMasterBo.getUserId());
                allLog.initEntity();
                allLog.setOrgId(drugImportMasterBo.getOrgId());
                DrugStockAllLogParamVo drugStockAllLogParamVo = new DrugStockAllLogParamVo();
                drugStockAllLogParamVo.setDrugStockAllLog(allLog);
                drugStockAllLogParamVo.setRecordType(DrugConstants.SEVEN);
                Boolean b = stockAllLogService.caseRecordTypeSave(drugStockAllLogParamVo);
            });
        }
    }
}






